<?

/* GameController controller class.
 * This class contains the handling code, divided into action methods.
 *
 */ 
class GameController extends AGController
{
  function __construct($ctrName) 
  {
    parent::__construct($ctrName);
    $this->invokeModel();
    $this->party = $this->getModel('party');
    $this->model->character = &$this->character;
    $this->model->parent = &$this;
  }

  function Index()
  {
    $this->currentZone = $this->character->charDS['c_location'];
    $this->model->loadZone($this->currentZone);
    $this->pageTitle = $this->model->zoneDS['z_name'];
    $this->zoneStats = TextToStringlist($this->model->zoneDS['z_stats']);
    $this->viewName = getDefault($this->zoneStats['zonetype'], 'index');
    $this->maxPartySize = 5;
    if ($_REQUEST['cmd'] == 'rest')
    {
      $this->character->stats['AP.current'] = $this->character->getCharacterStat('AP')-10;
      $this->character->regenerate(5);
      $this->character->saveCharacter();
    }
    if ($_REQUEST['move']!='')
    {
      if ($this->character->getCharacterStat('AP') >= 1)
        $this->model->mapMove($_REQUEST['move']);
      else
        $this->model->mapMessage= 'You do not have enough AP to move further!';
    }
    if (isset($_SESSION['lootMessage']))
    {
      print('<div class="banner">'.$_SESSION['lootMessage'].'</div>');
      unset($_SESSION['lootMessage']);
    }
  }
  
  function Act()
  {
    include_once('lib/combat.php');
    $this->selectedAction = $_REQUEST['id']+0;
    $this->model->loadZone($this->character->charDS['c_location']);
    $this->pageTitle = 'Kill zem!!!';
  }
  
  function _DoMove($act, $newZoneId, $noRedirect = false)
  {
    $this->character->stats['XP'] += $act['a_xp'];
    $this->character->stats['AP.current'] = 
      $this->character->getCharacterStat('AP') - $act['a_cost']; 
    $this->character->charDS['c_location'] = $newZoneId;
    $this->character->saveCharacter();
    if (!$noRedirect) $this->redirect('index', 'game');
  }
  
  function Move()
  {
    $this->model->loadZone($this->character->charDS['c_location']);
    $this->selectedAction = $_REQUEST['id']+0;
    foreach($this->model->getActionListForZone($this->selectedAction) as $act)
      if ($this->model->printPrerequisites($act['a_prereqs'], $this->character, true) && $act['a_type'] == 'Z')
    {
      $act_stats = TextToStringList($act['a_stats']);
      $this->_zoneLoot($act_stats);
      $this->_DoMove($act, $act_stats['zone']);
    }
    $this->redirect('index', 'game');
  }
  
  function MapMove($direction)
  {
    $this->currentZone = $this->character->charDS['c_location'];
    $this->model->loadZone($this->character->charDS['c_location']);
    $this->selectedAction = $_REQUEST['id']+0;
    $this->zoneStats = TextToStringlist($this->model->zoneDS['z_stats']);
    $mapDS = DB_GetDatasetMatch('mapstates', array(
      'ms_character' => $this->character->charDS['c_key'],
      'ms_zone' => $this->currentZone, 
      ));
    $this->mstats = TextToStringlist($mapDS['ms_state']);
    if ($this->mstats['currentlocation'] == $this->selectedAction &&
      $this->character->getCharacterStat('AP') >= 1)
    {
      $act_stats = TextToStringList('');
      $act = TextToStringList('');;
      $act_stats['zone'] = $this->selectedAction;
      $act['a_cost'] = 1;
      $this->_DoMove($act, $act_stats['zone']);
      return;
    }
    $this->redirect('index', 'game');
  }
  
  function _zoneLoot($properties)
  {
    include_once('lib/combat.php');
    $ditems = dropItems($properties);
    $lm = $this->model->makeDropList($ditems);
    if ($lm != '')
      $_SESSION['lootMessage'] = $lm;  
  }
  
  function Quest()
  {
    include_once('lib/combat.php');
    $this->selectedAction = $_REQUEST['id']+0;
    $actionDS = DB_GetDataset('actions', $this->selectedAction);
    $properties = TextToStringList($actionDS['a_stats']);
    if($actionDS['a_zone'] != $this->character->charDS['c_location'])
      $this->redirect('index', 'game');
    $this->_zoneLoot($properties);
    switch($properties['subtype'])
    {
      case('C'):
      {
        $this->viewName = 'act';
        $this->Act();
        break;
      }
      case('Z'):
      {
        include_once('lib/combat.php');
        registerQuest($actionDS, $this->character);
        $this->viewName = 'move';
        $this->_DoMove($actionDS, $properties['zone']);
        break;
      }
    }
  }
  
  function ajax_selectspell()
  {
    global $config;
    $this->character->stats['spell.prepared'] = $_REQUEST['spell'];
    $this->character->saveCharacter();
    $this->skipView = true;
    $config['site.pagetemplate'] = 'blank';
  }  
}


?>
